package cn.hls.service.impl;

import java.util.Date;

import javax.annotation.Resource;

import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import cn.hls.dao.ACLDao;
import cn.hls.dao.RoleDao;
import cn.hls.dao.UserRoleDao;
import cn.hls.entity.ACL;
import cn.hls.entity.Role;
import cn.hls.entity.vo.PageInfo;
import cn.hls.exception.BussinessException;
import cn.hls.service.RoleService;
import cn.hlsconstant.DataAssessEnum;

/**
 *@fileName: RolrServiceBean.java
 *@package: com.bangni.service.impl
 *@Description:  角色管理Service接口实现类
 *@author: yinghui.zhang
 *@email: uuhui@163.com
 *@date: 2015-5-2
 */
@Service("roleService")
public class RoleServiceBean extends BaseServiceBean<Role> implements
		RoleService {

	@Resource(name = "roleDaoImpl")
	private RoleDao roleDao;
	@Resource(name = "userRoleDaoImpl")
	private UserRoleDao userRoleDao;
	@Resource(name="aclDaoImpl")
	private ACLDao aclDao;
	@Override
	public boolean update(Role entity) throws Exception {
		Role tempEntity=roleDao.find(entity.getId());
		BeanUtils.copyProperties(entity, tempEntity);
		tempEntity.setUpdateTime(new Date());
		return super.update(tempEntity);
	}
	@Override
	public PageInfo<Role> loadRoleInfo(Role entity, PageInfo<Role> page)
			throws Exception {
		return roleDao.loadRoleInfo(entity, page);
	}

	@Override
	public boolean checkRoleCode(String code) throws Exception {
		return roleDao.checkRoleCode(code);
	}

	@Override
	public boolean deleteRole(Long id) throws Exception {
		try {
			userRoleDao.deleteUserRole(id, null);
			// to-do 删除用户的acl授权
			aclDao.delPermission(ACL.TYPE_ROLE, id);
			roleDao.delete(id);
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			throw new BussinessException(DataAssessEnum.DELETE_ERROR,"删除角色信息失败");
		}
	}

}
